package com.put.mybills.activities;


import java.util.ArrayList;
import java.util.List;
import com.put.mybills.R;
import com.put.mybills.adapter.GlobalParams;
import com.put.mybills.adapter.FactureListAdapter;
import com.put.mybills.model.Facture;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class MyFactureFragment extends Fragment{
	
	private static final String TAG = "MyBillsFragment";	
	private ListView myFactureList;
	private List<Facture> allFactures;
	private FactureListAdapter myFactureListAdapter;
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {				
		inflater.inflate(R.menu.main_add_new_facture, menu);
		super.onCreateOptionsMenu(menu,inflater);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
	        case R.id.new_facture:
        	    Intent intent = new Intent(getActivity(), AddNewFactureActivity.class);
        	    //EditText editText = (EditText) findViewById(R.id.edit_message);
        	    //tring message = editText.getText().toString();
        	    //intent.putExtra(EXTRA_MESSAGE, message);
        	    startActivity(intent);
	            return true;	     
		}

		return super.onOptionsItemSelected(item);
	}
	

	@Override
	public void onResume() {			
		
		allFactures = GlobalParams.getInstance().facturedatasource.getAllFactures();
		List<String> factureName = new ArrayList<String>();
		
		for (int i=0;  i < allFactures.size() ; i++)
			factureName.add(allFactures.get(i).getName());
		
		String[] values =  factureName.toArray(new String[factureName.size()]);
		
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());	
		Boolean switcher = sharedPreferences.getBoolean("read_facture_icon", false);		
		myFactureListAdapter = new FactureListAdapter(getActivity(), values,switcher);
		myFactureListAdapter.setFactures(allFactures);
		myFactureList.setAdapter(myFactureListAdapter);			
		
		super.onResume();
	}
	
	
	


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View rootView = inflater.inflate(R.layout.fragment_myfacture, container, false);
		
		myFactureList = (ListView) rootView.findViewById(R.id.list_myfactues); 	
		setHasOptionsMenu(true);
		
		allFactures = GlobalParams.getInstance().facturedatasource.getAllFactures();
		List<String> factureName = new ArrayList<String>();
		
		for (int i=0;  i < allFactures.size() ; i++)
			factureName.add(allFactures.get(i).getName());
		
		String[] values =  factureName.toArray(new String[factureName.size()]);
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());	
		Boolean switcher = sharedPreferences.getBoolean("read_facture_icon", false);
		myFactureListAdapter = new FactureListAdapter(getActivity(), values,switcher);
		myFactureListAdapter.setFactures(allFactures);
		myFactureList.setAdapter(myFactureListAdapter);	
		
		
		myFactureList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {			
				
				String item = (String) (myFactureList.getItemAtPosition(position));				
				Intent intent = new Intent(getActivity(), FactureActivity.class);	        	 
	        	intent.putExtra("FactureName", item);
	        	getActivity().startActivity(intent);  
			}});
			
		return rootView;
			
	}
}
